interface StateModel {
	tabsData: any[];
	tabsValue: '';
}
const state = {
	tabsData: [],
	tabsValue: '1'
};

const getters = {
	getTabsValue(state: StateModel): string {
		return state.tabsValue;
	},
	getTabsData(state: StateModel): any[] {
		return state.tabsData;
	}
};

//异步方法
const actions = {};

//同步方法
const mutations = {
	addTabs(state: StateModel, tab: any): void {
		//判断是否已存在要添加的tab的name
		const exist = state.tabsData.filter((tabs) => tabs.name === tab.name);
		if (exist.length > 0) {
			//存在则选中tab也
			state.tabsValue = exist[0].name;
		} else {
			//不存在则添加tab
			state.tabsData.push(tab);
		}
	},
	/**
	 * 更改tab的选中状态
	 * @param state vuex的属性
	 * @param value 要选中tab的name值
	 */
	changeTabsValue(state: StateModel, value: any) {
		state.tabsValue = value;
	},
	/**
	 * 删除tab页
	 * @param state vuex的属性
	 * @param targetName  要删除tab的name值
	 */
	removeTab(state: StateModel, targetName: any) {
		if (state.tabsValue === targetName) {
			state.tabsData.forEach((tab, index) => {
				if (tab.name === targetName) {
					const nextTab = state.tabsData[index + 1] || state.tabsData[index - 1];
					//删除后根据情况自动选中下一个或者上一个tab页
					if (nextTab) {
						state.tabsValue = nextTab.name;
						//console.log(nextTab.name);
					}
				}
			});
		}
		state.tabsData = state.tabsData.filter((tab) => tab.name !== targetName);
	}
};

export default {
	namespaced: true,
	state,
	getters,
	actions,
	mutations
};
